import { NodeSize } from '../config'

export default function textFileOutput(flow) {
  flow.register('TextFileOutput', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M350.232381 427.934476h290.182095v19.529143H350.232381zM223.036952 141.263238v75.166476L95.646476 89.039238 62.878476 121.807238l127.414857 127.390476H113.590857v46.348191l155.794286-0.024381V141.263238z'
            }
          ),
          h(
            'path',
            {
              fill: '#EFF9FE',
              d: 'M279.600762 817.761524h431.396571l-1.414095-491.324953h-103.789714V222.622476h-152.405334l-173.787428 173.787429v421.351619z m70.631619-389.827048h290.182095v19.529143H350.232381v-19.529143z m0 82.480762h290.182095v19.529143H350.232381v-19.529143z m78.043429 82.480762v19.529143h-78.043429v-19.529143h78.043429z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M752.566857 859.184762l-1.511619-532.528762-41.691428-41.667048h-62.122667V222.866286l-41.691429-41.691429h-110.738285l-41.423239 41.447619h152.405334v103.789714h103.789714l1.414095 491.349334H279.600762V396.385524l-41.447619 41.472V859.184762z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M350.232381 510.415238h290.182095v19.529143H350.232381zM350.232381 592.896h78.043429v19.529143h-78.043429z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '文本文件输出'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
